package com.zcsy.commons.util;


import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.log4j.Logger;


/**
 * @Title: SmsUtil.java
 * @Package： com.zcsy.web.websocket
 * @Description: 发送短信工具类〈一句话功能简述〉 〈功能详细描述〉
 * @author wangziliang 460694546@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年7月1日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
public class SmsUtil {

    private static final Logger logger = Logger.getLogger(SmsUtil.class);

    private static String userName = null;//

    private static String password = null;
    private static String corpId = null;

    private static String wsUrl = null;

    public static final String SMS_USERNAME_ERR = "102";// 用户名密码不正确

    public static final String SMS_SYS_ERR = "101";// 系统故障

    public static final String SMS_TOTAL_AMOUNT_ERR = "3";// 所剩于的发送总量低于您现在的发送量

    public static final String SMS_MOBILE_NUM_ERR = "116";// 目的号码太长（最多100个）

    public static final String SMS_SUCCESS = "1";// 发送成功
    
    public static final String SMS_FAILE = "2";// 发送失败
    public static final String SMS_READYTOSEND = "0";// 准备发送

    private static Service service = new Service();// 实例化service

    private static Call call = null; // 短信第三方接口
    

    // 初始化数据
    static {
        userName = Globals.getConfig("sms_username");
        password = DigestUtil.MD5(Globals.getConfig("sms_password"));// 密码32为MD5加密
        corpId = Globals.getConfig("sms_corpid");
        wsUrl = Globals.getConfig("sms_wsurl");
        // 初始化短信ws接口
        try {
            createCall();
        } catch (ServiceException e) {
            logger.error(e, e);
        } catch (MalformedURLException e) {
            logger.error(e, e);
        }
    }

    /**
     * @Description 获取短信接口call对象<br>
     * @return Call 通过单例模式获取短信接口call对象
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public static Call getSmsCall() {
        if (call == null) {
            try {
                createCall();
            } catch (ServiceException e) {
                logger.error(e, e);
            } catch (MalformedURLException e) {
                logger.error(e, e);
            }
        }
        return call;
    }

    /**
     * @Description 创建call对象<br>
     * @throws ServiceException
     * @throws MalformedURLException
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    private static void createCall()
        throws ServiceException, MalformedURLException {
        call = (Call)service.createCall();
        call.setTargetEndpointAddress(new URL(wsUrl));
        call.setOperationName(new QName("", "sendMsg"));
        call.addParameter("corpId", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("userId", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("pwd", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("mobiles", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("content", XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
    }

    /**
     * @Description 发送短信 mobile 手机号码 message 短信内容<br>
     * @param mobile
     *            手机号
     * @param message
     * @return 返回值说明： 9-用户名密码不正确 8-系统故障 3-所剩于的发送总量低于您现在的发送量 2-超过今天的最大发送量 1-目的号码太长（最多100个）
     *         0-发送成功。成功返回。没有状态数据时返回 0:<MTPacks></MTPacks> 有状态时返回
     *         0:<MTPack><Id>123</Id><Mobile>18900006666
     *         </Mobile><Id>124</Id><Mobile>13958585555</Mobile></MTPack>
     * @throws Exception
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public static String sendSms(String mobile, String message)
        throws Exception {
        Long start = System.currentTimeMillis();
        logger.info("begin call SendSms Interface...");
        Call call = getSmsCall();
        String result = (String)call.invoke("sendMsg", new Object[] {corpId,userName, password, mobile,
            message});
        logger.info("call SendSms result : " + result);
        Long end = System.currentTimeMillis();
        logger.info("call SendSms cost time :" + (end - start));
        logger.info("end call SendSms Interface...");
        return result;
    }

    /**
     * @Description 获取状态报告 成功返回。没有状态数据时返回 0:<StatusPacks></StatusPacks> 有状态时返回<br>
     * @return 0:<StatusPacks><StatusPack><Id>123
     *         </Id><Status>UNDELIV</Status><ErrCode>MK:0004</ErrCode></StatusPack
     *         ><StatusPack><Id>124</Id><
     *         Status>DELIVRD</Status><ErrCode>DELIVRD</ErrCode></StatusPack></StatusPacks>
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws RemoteException
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public static String getStatus()
        throws ServiceException, MalformedURLException, RemoteException {
        Long start = System.currentTimeMillis();
        logger.info("begin call GetStatus Interface...");
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setOperationName(new QName("", "GetStatus"));
        call.addParameter("UserName", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("Password", XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        call.setTargetEndpointAddress(new URL(wsUrl));
        String result = (String)call.invoke(new Object[] {userName, password});
        logger.info("call GetStatus result : " + result);
        Long end = System.currentTimeMillis();
        logger.info("call GetStatus cost time :" + (end - start));
        logger.info("end call GetStatus Interface...");
        return result;
    }

    /**
     * @Description 获取用户回复短信(即上行短信)<br>
     * @return
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws RemoteException
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public static String getMoSms()
        throws ServiceException, MalformedURLException, RemoteException {
        Long start = System.currentTimeMillis();
        logger.info("begin call GetMONotDel Interface...");
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setOperationName(new QName("", "GetMONotDel"));
        call.addParameter("UserName", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("Password", XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        call.setTargetEndpointAddress(new URL(wsUrl));
        String result = (String)call.invoke(new Object[] {userName, password});
        logger.info("call GetMONotDel result : " + result);
        Long end = System.currentTimeMillis();
        logger.info("call GetMONotDel cost time :" + (end - start));
        logger.info("end call GetMONotDel Interface...");
        return result;
    }

    /**
     * @Description 删除用户回复短信<br>
     * @param id
     *            调用GetMONotDel方法取得ID号，之间用英文半角分号隔开，如："123;124"
     * @return
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws RemoteException
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public static String delMo(String id)
        throws ServiceException, MalformedURLException, RemoteException {
        Long start = System.currentTimeMillis();
        logger.info("begin call DelMo Interface...");
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setOperationName(new QName("", "DelMo"));
        call.addParameter("UserName", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("Password", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("Id", XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        call.setTargetEndpointAddress(new URL(wsUrl));
        String result = (String)call.invoke(new Object[] {userName, password, id});
        logger.info("call DelMo result : " + result);
        Long end = System.currentTimeMillis();
        logger.info("call DelMo cost time :" + (end - start));
        logger.info("end call DelMo Interface...");
        return result;
    }

    /**
     * @Description 获取账号余额信息<br>
     * @return 成功返回。没有数据时返回 0:<RemainSms></RemainSms> 有数据时返回
     *         0:<RemainSms><Remain>0</Remain></RemainSms>
     * @throws ServiceException
     * @throws MalformedURLException
     * @throws RemoteException
     * @author wangziliang 46094546@qq.com
     * @date 2015年7月1日
     * @see
     */
    public static String getRemainSms()
        throws ServiceException, MalformedURLException, RemoteException {
        Long start = System.currentTimeMillis();
        logger.info("begin call GetRemainSms Interface...");
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setOperationName(new QName("", "balance"));
        call.addParameter("corpId", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("userId", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("pwd", XMLType.XSD_STRING, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        call.setTargetEndpointAddress(new URL(wsUrl));
        String result = (String)call.invoke(new Object[] {corpId,userName, password});
        logger.info("call GetRemainSms result : " + result);
        Long end = System.currentTimeMillis();
        logger.info("call GetRemainSms cost time :" + (end - start));
        logger.info("end call GetRemainSms Interface...");
        return result;
    }

}
